package com.supersit.GZFDA.util;

import java.io.File;
import java.nio.charset.Charset;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.apache.http.util.TextUtils;

import com.supersit.GZFDA.db.DBUtils;
import com.supersit.GZFDA.entity.Business;
import com.supersit.GZFDA.receiver.NetworkConnection;
import com.supersit.GZFDA.util.CustomMultipartEntity.ProgressListener;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

public class HttpMultipartPost extends AsyncTask<String, Integer, String> {

	private Context context;
	private String filePath[];
	private ProgressDialog pd;
	private long totalSize;
	private String requestUrl;
	private GetResult mGetResult;
	private String QYMC;

	public HttpMultipartPost(Context context, String[] filePath,
			String requestUrl, GetResult mGetResult, String QYMC) {
		this.context = context;
		this.filePath = filePath;
		this.requestUrl = requestUrl;
		this.mGetResult = mGetResult;
		this.QYMC = QYMC;
	}

	@Override
	protected void onPreExecute() {
		pd = new ProgressDialog(context);
		pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		pd.setMessage("...正在上传图片...");
		pd.setCancelable(false);
		pd.show();
	}

	@SuppressWarnings("deprecation")
	@Override
	protected String doInBackground(String... params) {
		String serverResponse = null;
		HttpClient httpClient = new DefaultHttpClient();
		HttpContext httpContext = new BasicHttpContext();
		HttpPost httpPost = new HttpPost(requestUrl);

		try {
			CustomMultipartEntity multipartContent = new CustomMultipartEntity(
					new ProgressListener() {
						@Override
						public void transferred(long num) {
							publishProgress((int) ((num / (float) totalSize) * 100));
						}
					});
			@SuppressWarnings("deprecation")
			// ContentBody cb = new StringBody(QYMC);
			Business business = DBUtils.getBusiness(QYMC, context);
			String name = "";
			if (TextUtils.isEmpty(business.getBusiness_name())) {
				name = "_" + QYMC + "_";
			} else {
				name = business.getBusiness_type() + "_"
						+ business.getBusiness_name() + "_"
						+ business.getLicense_key();
			}

			ContentBody cb = new StringBody(name, Charset.forName("UTF-8"));
			// 上传企业名称
			multipartContent.addPart("username", cb);

			// 使用FileBody上传图片
			for (int i = 0; i < filePath.length; i++) {

				multipartContent.addPart("file" + i, new FileBody(new File(
						filePath[i])));
			}
			totalSize = multipartContent.getContentLength();
			// 上传
			httpPost.setEntity(multipartContent);
			HttpResponse response = httpClient.execute(httpPost, httpContext);
			serverResponse = EntityUtils.toString(response.getEntity());
			// System.out.println(serverResponse);
		} catch (Exception e) {
			pd.dismiss();
			e.printStackTrace();
		}
		return serverResponse == null ? "" : serverResponse;
	}

	@Override
	protected void onProgressUpdate(Integer... progress) {
		pd.setProgress((int) (progress[0]));
	}

	@Override
	protected void onPostExecute(String result) {
		mGetResult.onPostExecute(result);
		System.out.println("result: " + result);
		pd.dismiss();
	}

	@Override
	protected void onCancelled() {
		System.out.println("cancle");
	}

	public interface GetResult {
		public void onPostExecute(String result);
	}
}
